/*
 * Copyright (c) 2008 VMware, Inc.
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.wtk.skin.terra;

import wtkx.Button;
import wtkx.Component;
import wtkx.MenuBar;
import wtkx.MenuBarItem;
import wtkx.wtk.Cursor;
import wtkx.wtk.Dimensions;
import wtkx.wtk.skin.MenuBarItemSkin;

import java.awt.Color;
import java.awt.Graphics2D;

/**
 * Terra menu bar item skin.
 *
 * @author gbrown
 */
public class TerraMenuBarItemSkin
    extends MenuBarItemSkin
{

    public TerraMenuBarItemSkin(){
        super();
    }


    @Override
    public void install(Component component) {
        super.install(component);

        MenuBarItem menuBarItem = (MenuBarItem)component;
        menuBarItem.setCursor(Cursor.DEFAULT);
    }

    public int getPreferredWidth(int height) {
        MenuBarItem menuBarItem = (MenuBarItem)getComponent();

        Button.DataDecorator dataDecorator = menuBarItem.getDataDecorator();
        dataDecorator.render(menuBarItem.getButtonData(), menuBarItem, false);

        return dataDecorator.getPreferredWidth(height);
    }

    public int getPreferredHeight(int width) {
        MenuBarItem menuBarItem = (MenuBarItem)getComponent();

        Button.DataDecorator dataDecorator = menuBarItem.getDataDecorator();
        dataDecorator.render(menuBarItem.getButtonData(), menuBarItem, false);

        return dataDecorator.getPreferredHeight(width);
    }

    public Dimensions getPreferredSize() {
        MenuBarItem menuBarItem = (MenuBarItem)getComponent();

        Button.DataDecorator dataDecorator = menuBarItem.getDataDecorator();
        dataDecorator.render(menuBarItem.getButtonData(), menuBarItem, false);

        return dataDecorator.getPreferredSize();
    }

    public void layout() {
    }

    public void paint(Graphics2D graphics) {
        MenuBarItem menuBarItem = (MenuBarItem)getComponent();
        MenuBar menuBar = menuBarItem.getMenuBar();

        int width = getWidth();
        int height = getHeight();

        boolean highlight = menuPopup.isOpen();


        if (highlight) {
            Color highlightBackgroundColor = (Color)menuBar.getStyles().get("highlightBackgroundColor");
            graphics.setColor(highlightBackgroundColor);
            graphics.fillRect(0, 0, width, height);
        }


        Button.DataDecorator dataDecorator = menuBarItem.getDataDecorator();
        dataDecorator.render(menuBarItem.getButtonData(), menuBarItem, highlight);
        dataDecorator.setSize(width, height);

        dataDecorator.paint(graphics);
    }

    public Color getPopupBorderColor() {
        return (Color)menuPopup.getStyles().get("borderColor");
    }

    public void setPopupBorderColor(Color popupBorderColor) {
        menuPopup.getStyles().put("borderColor", popupBorderColor);
    }

    public void setPopupBorderColor(String popupBorderColor) {
        if (popupBorderColor == null) {
            throw new IllegalArgumentException("popupBorderColor is null.");
        }

        menuPopup.getStyles().put("borderColor", popupBorderColor);
    }
}
